package com.yzr.common.collection.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @description hashMap7大遍历方式
 * @author: yx-0173
 * @date: 2021-08-09 15:16
 **/
public class HashMapConnect {
    public static void main(String[] args) {
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("1", "one");
        hashMap.put("2", "two");
        hashMap.put("3", "three");
        hashMap.put("4", "four");
        hashMap.put("5", "five");


        Set<Object> keySet = hashMap.keySet();
        // 增强
        for (Object obj : keySet) {
            System.out.println(hashMap.get(obj));
        }

        System.out.println();
        System.out.println("==========迭代器==========");
        // 迭代器
        Iterator<Object> iterator = keySet.iterator();
        while (iterator.hasNext()) {
            System.out.println(hashMap.get(iterator.next()));
        }

        System.out.println();
        System.out.println("==========values==========");
        // 获取所有的value
        Collection<Object> values = hashMap.values();
        // 增强for
        for (Object obj : values) {
            System.out.println(obj);
        }
        // 迭代器
        Iterator<Object> valueIterator = values.iterator();
        while (valueIterator.hasNext()) {
            System.out.println(valueIterator.next());
        }

        System.out.println("=============entitySet============");
        Set<Map.Entry<Object, Object>> entries = hashMap.entrySet();
        for (Map.Entry<Object, Object> entryMap : entries) {
            System.out.println("key=" + entryMap.getKey());
            System.out.println("value=" + entryMap.getValue());
        }
        System.out.println("=============entitySet迭代器===========");
        Iterator<Map.Entry<Object, Object>> entitySet = entries.iterator();
        while (entitySet.hasNext()) {
            Map.Entry<Object, Object> next = entitySet.next();
            System.out.println("key=" + next.getKey());
            System.out.println("value=" + next.getValue());
        }
//        System.out.println(1 << 30);
////        System.out.println(2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2);
//        System.out.println(60 >> 2);

        System.out.println("================foreach=================");
        hashMap.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(v);
        });
    }
}
